const request = require("request");
var client = require("../db/momo/MonoDb");

module.exports = {
  UserMonitorByAPI: async () => {
    var result = await new Promise((resolve, reject) => {
      //批量获取所有设备
      var url =
        "http://www.gpscj.com/api/UserMonitorByAPI.aspx?loginName=Bijay&password=123456&mapType=baidu&key=2020YCHANGJIAN8SH3";
      request(url, function (err, response, body) {
        if (!err && response.statusCode == 200) {
          console.log(typeof body);

          var data = JSON.parse(body);

          client.connect(function (err) {
            if (err) throw err;
            var dbo = client.db("runoob");

            dbo.collection("gps").insertMany(data.devices, function (err, res) {
              if (err) throw reject(err);
              resolve(res.ops);
            });
          });
        }
      });
    });
    return result;
  },
};
